<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header height='60px'>
      <div>
        <img alt="" src="@/assets/logo.png"/>
        <span>指标管理系统</span>
        <span>当前角色为: {{ role }}</span>
      </div>
      <div>
        <span>你好, {{ userName }}</span>
        <el-image
            fit="fit"
            src="https://tse4-mm.cn.bing.net/th/id/OIP-C.ytZ3w0NP3JfGHzPzNXO25QAAAA?pid=ImgDet&rs=1"
            style="width: 50px; height: 50px; margin: 0 30px; cursor: pointer;">
        </el-image>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏菜单区 -->
        <el-menu
            :default-active="activePath"
            :router="true"
            active-text-color="#409eff"
            background-color="#333744"
            text-color="#fff"
            unique-opened
        >
          <!-- 一级菜单 -->
          <!-- index是唯一标识，而且类型必须是字符串，如果不加会全部展开 -->
          <el-menu-item
              v-for="item in menulist"
              :key="item.menuId"
              :index="item.pathName"
              @click="saveNavState(item.pathName)">
            <i class="el-icon-setting"></i>
            <span slot="title">{{ item.menuName }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menulist: [],
      activePath: '',
      role: '', // 角色：管理员、普通用户
      userName: '', // 用户名
      dialogVisible: false,
      userInfo: {}
    }
  },
  created() {
    this.getMenuList()  //获取菜单列表
    this.activePath = '/'
  },
  methods: {
    logout() {
      this.$router.push('/login')
    },
    //获取菜单
    async getMenuList() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      this.userInfo = userInfo
      this.userName = userInfo.username

      const res = await this.$http.get(`menus/all?userId=${userInfo.id}`)

      if (res.code !== 200 || res.code !== 202) {
        this.menulist = res.data
        this.role = res.message
      } else {
        return this.$message.error(res.message)
      }
    },
    saveNavState(path) {
      window.localStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  height: 80px;
  background-color: #373d41;
  display: flex;
  justify-content: space-between; //主轴 空隙在中间
  // padding-left: 10px;
  align-items: center; //垂直居中
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
      color: #fff;
    }

    img {
      width: 54px;
      display: inline-block;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
</style>